package com.example.administrator.baikangxing.activity;

import android.content.Intent;
import android.graphics.Color;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.administrator.baikangxing.Dao.OperateData;
import com.example.administrator.baikangxing.R;
import com.example.administrator.baikangxing.bean.StepBean;
import com.example.administrator.baikangxing.config.Constants;
import com.example.administrator.baikangxing.config.Url;
import com.example.administrator.baikangxing.utils.CommomUtil;
import com.example.administrator.baikangxing.utils.HttpUtil;
import com.example.administrator.baikangxing.utils.LogUtil;
import com.example.administrator.baikangxing.utils.ToastUtil;
import com.example.administrator.baikangxing.utils.Utils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import lecho.lib.hellocharts.gesture.ZoomType;
import lecho.lib.hellocharts.listener.ColumnChartOnValueSelectListener;
import lecho.lib.hellocharts.model.Axis;
import lecho.lib.hellocharts.model.AxisValue;
import lecho.lib.hellocharts.model.Column;
import lecho.lib.hellocharts.model.ColumnChartData;
import lecho.lib.hellocharts.model.SubcolumnValue;
import lecho.lib.hellocharts.view.ColumnChartView;

public class StepActivity extends BaseActivity implements View.OnClickListener {
    ColumnChartData columnData;
    private LinearLayout step_ll_statistics;
    private TextView steps_tv_number;
    private TextView steps_tv_number_two;
    private TextView steps_tv_mileage;
    private TextView steps_tv_carle;
    private ColumnChartView steps_chart;
    private String[] date;
    private int[] score;

    @Override
    public int getContentViewLayoutId() {
        return R.layout.activity_step;
    }

    @Override
    public void initView() {
        base_ll_title.setBackgroundResource(R.color.steps_bg);
        setTitles("步数");
        setRightIcon(R.drawable.right_share);
        step_ll_statistics = findView(R.id.step_ll_statistics);
        steps_tv_number = findView(R.id.steps_tv_number);
        steps_tv_number_two = findView(R.id.steps_tv_number_two);
        steps_tv_mileage = findView(R.id.steps_tv_mileage);
        steps_tv_carle = findView(R.id.steps_tv_carle);
        steps_chart = findView(R.id.steps_chart);
        steps_chart.setValueSelectionEnabled(true);
        step_ll_statistics.setOnClickListener(this);
        base_ib_menu.setOnClickListener(this);
    }

    @Override
    public void initListener() {

    }

    @Override
    public void initData() {
        Intent intent = getIntent();
        String Passometer = intent.getStringExtra(Constants.HEALTH_TARGET[4]);
        steps_tv_number.setText(Passometer);
        steps_tv_number_two.setText(Passometer);
        int counts = Integer.parseInt(Passometer);
        String format = Utils.calc(0, counts);
        steps_tv_mileage.setText("" + format + getResources().getString(R.string.text_distance_mark));
        String carle = Utils.calc(1, counts);
        steps_tv_carle.setText(carle + getResources().getString(R.string.text_carle_mark));
        String select_num = OperateData.getStringData(this, Constants.SELECT_DEVICE_NUM);
        String[] values = new String[]{select_num,"hour","0","23"};
        String url = Url.base_url + Constants.HEALTH_TARGET[4];
        HttpUtil.getInstance().postString(url, new String[]{"devid", "ext", "begin", "end"}, values, new HttpUtil.CallBackPostString() {
            @Override
            public void onSuccess(JSONObject successString) {
                try {
                    if(successString.getString("errorCode").equals("0")){
                        JSONArray message = successString.getJSONArray("message");
                        ArrayList<StepBean> stepList = CommomUtil.parserJsonArrayToList(message, StepBean.class);
                        date = new String[stepList.size()];
                        score = new int[stepList.size()];
                        for (int i = 0; i < stepList.size(); i++) {
                            date[i] = stepList.get(i).getTime()+":00";
                            score[i] = Utils.getAbs(stepList.get(i).getPassometer());
                        }
                        setLine();
                    }else if(successString.getString("errorCode").equals("105")){
                        ToastUtil.showToast("暂无数据");
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFail(JSONObject failString) {

            }

            @Override
            public void onProgress(long bytesWritten, long totalSize) {

            }
        });
    }

    private void setLine() {

        int numSubcolumns = 1;
        int numColumns = date.length;

        List<AxisValue> axisValues = new ArrayList<AxisValue>();
        List<Column> columns = new ArrayList<Column>();
        List<SubcolumnValue> values;
        for (int i = 0; i < numColumns; ++i) {
            values = new ArrayList<SubcolumnValue>();
            for (int j = 0; j < numSubcolumns; ++j) {
                values.add(new SubcolumnValue(score[i],
                        Color.WHITE));
            }
            // 点击柱状图就展示数据量
            axisValues.add(new AxisValue(i).setLabel(date[i]));

            columns.add(new Column(values).setHasLabelsOnlyForSelected(true));
        }

        columnData = new ColumnChartData(columns);

        columnData.setAxisXBottom(new Axis(axisValues).setHasLines(false)
                .setTextColor(Color.BLACK).setMaxLabelChars(7));
        columnData.setAxisYLeft(new Axis().setHasLines(false)
                .setTextColor(R.color.steps_bg).setLineColor(R.color.steps_bg).setMaxLabelChars(0));

        steps_chart.setColumnChartData(columnData);

        // Set value touch listener that will trigger changes for chartTop.

        // Set selection mode to keep selected month column highlighted.
        steps_chart.setValueSelectionEnabled(true);

        steps_chart.setZoomType(ZoomType.HORIZONTAL);

        steps_chart.setOnValueTouchListener(new ColumnChartOnValueSelectListener() {
            @Override
            public void onValueSelected(int i, int i1, SubcolumnValue subcolumnValue) {
                LogUtil.e("点击的数据为："+subcolumnValue.getValue());
                steps_tv_number_two.setText((int)subcolumnValue.getValue()+"");
                String format = Utils.calc(0, (int)subcolumnValue.getValue());
                steps_tv_mileage.setText("" + format + getResources().getString(R.string.text_distance_mark));
                String carle = Utils.calc(1, (int)subcolumnValue.getValue());
                steps_tv_carle.setText(carle + getResources().getString(R.string.text_carle_mark));
            }

            @Override
            public void onValueDeselected() {

            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.step_ll_statistics:
                Intent intent = new Intent(this, StatisticsStepActivity.class);
                startActivity(intent);
                break;
            case R.id.base_ib_menu:
                Intent intent1 = new Intent(this, StepshareActivity.class);
                startActivity(intent1);
                break;
        }
    }

}
